package com.adityayadav.mini.server;

import java.util.HashMap;
import java.util.Iterator;

public class HTTPResponseCreator {

	private HashMap<String,String> headers = new HashMap<String,String>();
	private String httpVersion = null;
	private String responseStatus = null;
	private byte[] content = null;
	
	public void addHeader(String header, String value){
		headers.put(header, value);
	}
	
	public void removeHeader(String header){
		headers.remove(header);
	}

	public void setHttpVersion(String httpVersion) {
		this.httpVersion = httpVersion;
	}

	public void setResponseStatus(String responseStatus) {
		this.responseStatus = responseStatus;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}
	
	public String getMessage(){
		StringBuffer result = new StringBuffer();
		
		result.append(httpVersion);
		result.append(" ");
		result.append(responseStatus);
		result.append("\r\n");
		
		Iterator it = headers.keySet().iterator();
		while (it.hasNext()){
			String header = (String)it.next();
			result.append(header);
			result.append(": ");
			result.append((String) headers.get(header));
			result.append("\r\n");
		}
		
		result.append("\r\n");
		
		if (content!=null) result.append(new String(content));
		
		return result.toString();
	}
	
}
